﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Globalization;
using System.Data.SqlClient;
using System.Configuration;
using System.Data;

namespace Bus_proj.website
{
    public partial class Add_Around : System.Web.UI.Page
    {
        DataClasses1DataContext db = new DataClasses1DataContext();
        int z;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                try
                {
                    if (Int32.Parse(Session["login_Access_id"].ToString()) == 3)
                    {

                    }
                    else
                    {
                        Response.Redirect("login.aspx");
                    }
                }
                catch (Exception)
                {
                    Response.Redirect("login.aspx");
                }
                
            }       
        }

        public DateTime ConvestToDate(string s)
        {
            int day = 1;
            int mount = 1;
            int years = 1111;
            try
            {
                string[] stemp = s.Split(' ');
                stemp = stemp[0].Split('/');
                day = Convert.ToInt32(stemp[0]);
                mount = Convert.ToInt32(stemp[1]);
                years= Convert.ToInt32(stemp[2]);
                if (years > 2300)
                {
                    years = years - 543;
                }
                return new DateTime(years, mount, day);
            }
            catch (Exception)
            {

            }
            return new DateTime(years, mount, day);
        }

        public string ConvestToDate_check(string s)
        {
            int day = 1;
            int mount = 1;
            int years = 1111;
            try
            {
                string[] stemp = s.Split('-');
                years = Convert.ToInt32(stemp[0]);
                mount = Convert.ToInt32(stemp[1]);
                day = Convert.ToInt32(stemp[2]);
                if (years < 2400)
                {
                    years = years + 543;
                }
                string years2 = years.ToString();
                string mount2 = mount.ToString();
                string day2 = day.ToString();
                return (day2 + "/" + mount2 + "/" + years2);
            }
            catch (Exception)
            {

            }
            string years3 = years.ToString();
            string mount3 = mount.ToString();
            string day3 = day.ToString();
            return (years3 + "/" + mount3 + "/" + day3);
        }

        public string ConvestToDate_check22(string s)
        {
            int day = 1;
            int mount = 1;
            int years = 1111;
            try
            {
                string[] stemp = s.Split(' ');
                stemp = stemp[0].Split('/');
                day = Convert.ToInt32(stemp[0]);
                mount = Convert.ToInt32(stemp[1]);
                years = Convert.ToInt32(stemp[2]);
                if (years < 2400)
                {
                    years = years + 543;
                }
                string years2 = years.ToString();
                string mount2 = mount.ToString();
                string day2 = day.ToString();
                if (day2.Length ==1)
                {
                    day2 = "0" + day2;
                }
                if (mount2.Length == 1)
                {
                    mount2 = "0" + mount2;
                }
                return (years2 + "/" + mount2 + "/" +day2 );
            }
            catch (Exception)
            {

            }
            string years3 = years.ToString();
            string mount3 = mount.ToString();
            string day3 = day.ToString();
            return (years3 + "/" + mount3 + "/" + day3);
        }

        protected void Button1_Click(object sender, EventArgs e)
        {            
            DateTime datte = DateTime.Now;
            string sDate = datte.ToShortDateString();
            string check_date1 = ConvestToDate_check(TextBox1.Text);
            string check_date2 = ConvestToDate_check(TextBox2.Text); 
            DateTime ch_1 = Convert.ToDateTime(sDate);
            DateTime ch_2 = Convert.ToDateTime(check_date1);
            DateTime ch_3 = Convert.ToDateTime(check_date2);
            if ((ch_1 > ch_2 && ch_1 > ch_3) || (ch_1 > ch_2 && ch_1 < ch_3) || (ch_1 < ch_2 && ch_1 > ch_3))
            {
                Panel1.Visible = true;
                Panel2.Visible = false;
                Panel3.Visible = false;
                ScriptManager.RegisterClientScriptBlock(this.Page, this.Page.GetType(), "alert", "alert('ไม่สามารถเลือกวันที่นี้ได้');", true);
                //Response.Write(@"<script language='javascript'>alert('ไม่สามารถเลือกวันที่นี้ได้');</script>");
            }
            else
            {
                Session.Remove("round_chk");
                if (DateTime.Parse(TextBox1.Text) <= DateTime.Parse(TextBox2.Text) )
                {
                    Label9.Text = String.Format("{0:dddd dd MMMM yyyy}", DateTime.Parse(TextBox1.Text));
                    Label10.Text = String.Format("{0:dddd dd MMMM yyyy}", DateTime.Parse(TextBox2.Text));
                    string round_chk = DropDownList1.SelectedValue;
                    if (round_chk.Length == 1)
                    {
                        round_chk = "00" + round_chk;
                    }
                    else if (round_chk.Length == 2)
                    {
                        round_chk = "0" + round_chk;
                    }
                    Session["round_chk"] = round_chk;
                    Label12.Text = round_chk;
                    Panel1.Visible = false;
                    Panel2.Visible = true;
                    Panel3.Visible = true;
                }
                else
                {
                    Panel1.Visible = true;
                    Panel2.Visible = false;
                    Panel3.Visible = false;
                    ScriptManager.RegisterClientScriptBlock(this.Page, this.Page.GetType(), "alert", "alert('ไม่สามารถเพิ่มข้อมูลได้');", true);
                }
                
            }
            
        }

        protected void Button2_Click(object sender, EventArgs e)
        {
            int chk = 0;
            string search = @"select *
                            from Table_Around
                            where Around_no = '"+TextBox3.Text+"'";
            SqlCommand sqlcomm = new SqlCommand();
            string Connection = ConfigurationManager.ConnectionStrings["Bus_proConnectionString"].ConnectionString;
            SqlConnection sqlconn = new SqlConnection(Connection);
            SqlDataAdapter da = new SqlDataAdapter(search, sqlconn);
            DataTable dt = new DataTable();
            da.Fill(dt);
            if (dt.Rows.Count <= 0)
            {
                for (int count = 0; DateTime.Parse(TextBox1.Text).AddDays(count) <= DateTime.Parse(TextBox2.Text); count++)
                {
                    int i = 0;
                    foreach (GridViewRow gr in GridView1.Rows)
                    {
                        CheckBox CheckBox1 = (CheckBox)gr.FindControl("CheckBox1");
                        if (CheckBox1.Checked == true)
                        {
                            try
                            {
                                Select SData = new Select();
                                Table_Around x = new Table_Around();
                                z = SData.GetId_Around();
                                x.Around_id = z;
                                x.Around_no = TextBox3.Text;
                                x.Route_id = Int32.Parse(DropDownList1.SelectedValue);
                                string aa = ConvestToDate_check22(DateTime.Parse(TextBox1.Text).AddDays(count).ToString()).ToString();
                                string[] stemp = aa.Split(' ');
                                aa = stemp[0];
                                x.Around_date_chk = DateTime.Parse(aa);
                                aa = aa + " " + GridView1.Rows[i].Cells[3].Text + ":00.000";
                                x.Around_date = DateTime.Parse(aa);
                                x.Time_id = Int32.Parse(GridView1.Rows[i].Cells[1].Text);
                                x.Type_Bus_id = Int32.Parse(DropDownList2.SelectedValue);
                                x.Bus_Line_id = DropDownList3.SelectedValue;
                                db.Table_Arounds.InsertOnSubmit(x);
                                db.SubmitChanges();
                            }
                            catch (Exception)
                            {
                                Panel1.Visible = true;
                                Panel2.Visible = false;
                                Panel3.Visible = false;
                                ScriptManager.RegisterClientScriptBlock(this.Page, this.Page.GetType(), "alert", "alert('ไม่สามารถเพิ่มข้อมูลได้');", true);
                                //Response.Write(@"<script language='javascript'>alert('ไม่สามารถเพิ่มข้อมูลได้');</script>");
                            }
                        }
                        i++;
                    }
                }
            }
            else
            {
                for (int count = 0; DateTime.Parse(TextBox1.Text).AddDays(count) <= DateTime.Parse(TextBox2.Text); count++)
                {
                    int i = 0;
                    int chk_date = 0;
                    foreach (GridViewRow gr in GridView1.Rows)
                    {
                        CheckBox CheckBox1 = (CheckBox)gr.FindControl("CheckBox1");
                        if (CheckBox1.Checked == true)
                        {
                            try
                            {
                                Select SData = new Select();
                                Table_Around x = new Table_Around();
                                z = SData.GetId_Around();
                                string aa = ConvestToDate_check22(DateTime.Parse(TextBox1.Text).AddDays(count).ToString()).ToString();
                                string[] stemp = aa.Split(' ');
                                aa = stemp[0];
                                aa = aa + " " + GridView1.Rows[i].Cells[3].Text + ":00.000";
                                for (int v = 0; v < dt.Rows.Count; v++)
                                {                                   
                                    if (DateTime.Parse(dt.Rows[v]["Around_date"].ToString())== DateTime.Parse(aa))
                                    {
                                        chk_date++;
                                    }
                                }
                                if (chk_date<=0)
                                {
                                    //////////////////////
                                    x.Around_id = z;
                                    x.Around_no = TextBox3.Text;
                                    x.Route_id = Int32.Parse(DropDownList1.SelectedValue);
                                    x.Around_date_chk = DateTime.Parse(aa);
                                    x.Around_date = DateTime.Parse(aa);
                                    x.Time_id = Int32.Parse(GridView1.Rows[i].Cells[1].Text);
                                    x.Type_Bus_id = Int32.Parse(DropDownList2.SelectedValue);
                                    x.Bus_Line_id = DropDownList3.SelectedValue;
                                    db.Table_Arounds.InsertOnSubmit(x);
                                    db.SubmitChanges();
                                    //////////////////////
                                    chk_date = 0;
                                }
                                else
                                {
                                    chk_date = 0;
                                }
                            }
                            catch (Exception)
                            {
                                Panel1.Visible = true;
                                Panel2.Visible = false;
                                Panel3.Visible = false;
                                ScriptManager.RegisterClientScriptBlock(this.Page, this.Page.GetType(), "alert", "alert('ไม่สามารถเพิ่มข้อมูลได้');", true);
                                //Response.Write(@"<script language='javascript'>alert('ไม่สามารถเพิ่มข้อมูลได้');</script>");
                            }
                        }
                        i++;
                    }
                }
            }            
            ScriptManager.RegisterClientScriptBlock(this.Page, this.Page.GetType(), "alert", "alert('เพิ่มข้อมูลแล้ว');", true);
            //Response.Write(@"<script language='javascript'>alert('เพิ่มข้อมูลแล้ว');</script>");
            Panel1.Visible = true;
            Panel2.Visible = false;
            Panel3.Visible = false;
        }

        protected void Button3_Click(object sender, EventArgs e)
        {
            Panel1.Visible = true;
            Panel2.Visible = false;
            Panel3.Visible = false;
            Session.Remove("round_chk");
            Response.Redirect("Add_Around.aspx");
        }         
    }
}